/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * The login portlet
 */
qx.Class.define("bip.siteview.portlets.LoginPortlet", 
{
	extend: bip.views.Portlet,

	/*
	*****************************************************************************
	 EVENTS
	*****************************************************************************
	*/  
	events :
	{
		
		
	},

	/*
	*****************************************************************************
	 PROPERTIES
	*****************************************************************************
	*/ 
	properties: {
		
  	},

	/*
	*****************************************************************************
	 CONSTRUCTOR
	*****************************************************************************
	*/
	
	/**
	 * Construct the login portlet
	 * @param pageModel The page model of the portlet
	 */	
 	construct: function(pageModel)
	{
		this.base(arguments,pageModel, this.tr("Login"));
	
	},

	/*
	*****************************************************************************
	 MEMBERS
	*****************************************************************************
	*/	
	members:
	{	
		__container : null,
    __okButton : null,
    __nameField : null,
    __passwordField : null,
		
		/**
     * Create the content of the dialog. 
     * Extending classes must implement this method.
     */
    _createWidgetContent : function()
    {
    	this.setLayout(new qx.ui.layout.VBox(5));
    	/* Container layout */
      var layout = new qx.ui.layout.Grid(9, 5);
      layout.setColumnAlign(0, "right", "top");
      layout.setColumnAlign(2, "right", "top");

      /* Container widget */
      this.__container = new qx.ui.groupbox.GroupBox().set({
        contentPadding: [10, 10, 10, 10], legend:"Login"
      });
      this.__container.setLayout(layout);

      this.add(this.__container);

      /* Labels */
      var labels = [];
      labels.push(this.tr("Name"));
      labels.push(this.tr("Password"));
      for (var i=0; i<labels.length; i++) {
        this.__container.add(new qx.ui.basic.Label(labels[i]).set({
          allowShrinkX: true,
          paddingTop: 3
        }), {row: i, column : 0});
      }

      /* Text fields */
      this.__nameField = new qx.ui.form.TextField();
      this.__passwordField = new qx.ui.form.PasswordField();

      this.__container.add(this.__nameField.set({
        allowShrinkX: true,
        paddingTop: 3
      }), {row: 0, column : 1});

      this.__container.add(this.__passwordField.set({
        allowShrinkX: true,
        paddingTop: 3
      }), {row: 1, column : 1});

      /* Button */
      var button1 = this.__okButton =  new qx.ui.form.Button(this.tr("Login"));
      button1.setAllowStretchX(false);
      var newAccountButton =  new qx.ui.form.Button(this.tr("New Account"));
      newAccountButton.setAllowStretchX(false);

      this.__container.add(button1,{ row : 3, column : 1});
      this.add(newAccountButton);

      /* Check input on click */
      button1.addListener("execute", this.login, this);
      newAccountButton.addListener("execute", this.newAccount, this);
     
    },
    
		
		_beforeShow: function() {
			
	    },
	    
	
	    
	    newAccount: function() {
	    	qx.io.PartLoader.require(["newaccount"],function() {
	    		bip.ui.users.WinNewAccount.getInstance().addListener("newAccount",function(evt) {
	    			var data = evt.getData();
	    			this.__nameField.setValue(data["userid"]);
	    			this.__passwordField.setValue(data["password"]);
	    			bip.ui.users.WinNewAccount.getInstance().close();
	    			this.login();
	    		},this);
	  			bip.ui.users.WinNewAccount.getInstance().open();
	  	},this);
	    },
		
	    

		/*
	  	*****************************************************************************
	     UI
	  	*****************************************************************************
	 	*/
    /**
     * Login to the application, called on login button press
     */
    login : function(evt)
    {
    	// create the request
      this.__request = new qx.io.remote.Request("j_security_check", "POST");
      this.__request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      this.__request.setParameter("j_username", this.__nameField.getValue());
      this.__request.setParameter("j_password", this.__passwordField.getValue());
      // register the internal even before the user has the change to
      // register its own event in the delegate
      this.__request.addListener(
        "completed", this.__loginCompleteHandler, this
      );
      
      // mapp the state to its own state
      this.__request.addListener("changeState", function(ev) {
        var state = ev.getData();
        //this.setState(state);
        if (state === "failed" || state === "aborted" || state === "timeout") {
        	alert("Connection error, please try again "+state);
          this.fireEvent("error");
        }
      }, this);
      
      this.__request.send();
    	/*alert(this.__nameField.getValue()+" "+this.__passwordField.getValue());
      */
    },
    
    /**
     * Handler for the completion of the login request
     *
     * @param ev {qx.io.remote.Response} The event fired by the request.
     */
    __loginCompleteHandler : function(ev)
    {
       var data = ev.getContent();
       if (data.search(/LOGIN_ERROR/i) > 0) {
      	 alert("Wrong username/password");
       } else {
      	 qx.core.Init.getApplication().mainView();
       }
       
       // fire complete event
       //this.fireDataEvent("loaded", this.getModel());
    }
		

	},
	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects("__container","__okButton","__nameField","__passwordField");
		
	}
	
});

